const common = require('./common')

/**
 * Seneca插件包装类
 * @author tqboss
 * @module framework/plugin
 * @param {seneca对象} context
 * @param {服务名} serviceName
 * @returns {插件包装类}
 * @version 1.0.0
 */
exports.wrap = function (context, serviceName) {
  context.add(`service:${serviceName}`, function (msg, respond) {
    context.act(common.stringify(msg), function (error, result) {
      if (error) {
        return respond(error)
      }
      return respond(null, result)
    })
  })
  const WrapPlugin = function (ctx, name) {
    this.ctx = ctx
    this.name = name
    this.add = function (pattern, callback) {
      const p = common.createServicePin(this.name, pattern)
      this.ctx.add(p, callback)
    }
  }
  return new WrapPlugin(context, serviceName)
}
